//
//  PicTableViewCell.m
//  XRSquare
//
//  Created by 穆相臣 on 2018/11/16.
//  Copyright © 2018 UaenaMo. All rights reserved.
//

#import "PicTableViewCell.h"
#import "JSONKit.h"

@interface PicTableViewCell ()
{
    UILabel * titleLabel;
    UIScrollView * scrollContent;
    UIView * line;
}
@end

@implementation PicTableViewCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if ([super initWithStyle:style reuseIdentifier:reuseIdentifier])
    {
        titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, ScreenWidth, 20)];
        titleLabel.font = [UIFont boldSystemFontOfSize:15];
        titleLabel.text = @"企业图片";
        titleLabel.textColor = [UIColor colorFromHexCode:@"a3a3a3"];
        [self addSubview:titleLabel];
        
        scrollContent = [[UIScrollView alloc]initWithFrame:CGRectMake(10, 40, ScreenWidth, 90)];
        scrollContent.showsVerticalScrollIndicator = NO;
        scrollContent.showsHorizontalScrollIndicator = NO;
        [self addSubview:scrollContent];
        
        line = [[UIView alloc]initWithFrame:CGRectMake(0, 140, ScreenWidth, 10)];
        line.backgroundColor = [UIColor colorFromHexCode:@"f3f3f3"];
        [self addSubview:line];
    }
    return self;
}

+(CGFloat)heightWithData:(NSString *)data
{
    if (data && ![data isKindOfClass:[NSNull class]] && [[data objectFromJSONString]count])
    {
        return 150;
    }
    else
    {
        return 0;
    }
}

-(void)loadData:(NSString *)data
{
    if (data && ![data isKindOfClass:[NSNull class]] && [[data objectFromJSONString]count])
    {
        if (scrollContent)
        {
            [scrollContent removeFromSuperview];
        }
        scrollContent = [[UIScrollView alloc]initWithFrame:CGRectMake(10, 40, ScreenWidth, 90)];
        scrollContent.showsVerticalScrollIndicator = NO;
        scrollContent.showsHorizontalScrollIndicator = NO;
        [self addSubview:scrollContent];
        
        NSArray * imageArr = [data objectFromJSONString];
        
        
        for (int i = 0; i < [imageArr count]; i ++)
        {
            UIImageView * contentImage = [[UIImageView alloc]initWithFrame:CGRectMake(100 * i, 0, 90, 90)];
            NSURL * imageUrl = [NSURL URLWithString:[NSString stringWithFormat:@"https://pic.xrsquare.com/%@",[imageArr objectAtIndex:i]]];
            [contentImage sd_setImageWithURL:imageUrl];
            [scrollContent addSubview:contentImage];
        }
        scrollContent.contentSize = CGSizeMake(100 * [imageArr count], 90);
        for (UIView * view in self.subviews)
        {
            view.hidden = NO;
        }
    }
    else
    {
        for (UIView * view in self.subviews)
        {
            view.hidden = YES;
        }
    }
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
